package customerEnter.Frame.conntroller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ModelAttribute;

import com.alibaba.fastjson.JSON;

import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.bean.DMLSuccessOrFailure;
import customerEnter.tool.WhetherToEmpty;

/**
 * 
 * 
 * 类名称：BaseConntrollerFrame 类描述： 创建人：dy 创建时间：2016-7-22 上午10:51:35
 * 
 */
public class BaseConntrollerFrame {

	protected HttpServletRequest				request;
	protected HttpServletResponse				response;
	protected HttpSession						session;

	@SuppressWarnings("rawtypes")
	protected List<?>							ConntroList			= new ArrayList();

	protected Map<String, List<?>>				ConntroJsonMap		= new HashMap<String, List<?>>();

	protected Map<String, Object>				ConntroJsonObject	= new HashMap<String, Object>();

	protected BaseConntrollerFrameConntroView	ConntroView;

	protected ConntrollerFrameModel				frameModel;

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;

		response.setHeader("Access-Control-Allow-Origin", "*");
		this.session = request.getSession();
		ConntroView = new BaseConntrollerFrameConntroView(request, response);
		frameModel = new ConntrollerFrameModel(ConntroView, ConntroJsonObject);
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setHeader("contentType", "text/html; charset=utf-8");
		} catch (UnsupportedEncodingException e) {
			Log4j.setLog4j(this.getClass());
			Log4j.Log4JOutError("設置編碼失敗");
		}
	}

	/**
	 * 數據集合 map 的key 一定要是string 否則可能出現異常
	 */
	public void sendOutPrint() {
		try {

			PrintWriter out = response.getWriter();
			StringBuffer string = new StringBuffer();
			// JSONObject json = new JSONObject();
			// JSONArray jsonArray = new JSONArray();
			//
			// for (Entry<String, List<?>> map : ConntroJsonMap.entrySet()) {
			// // json.put(map.getKey(), map.getValue());
			// jsonArray = JSONArray.fromObject(map.getValue());
			// json.element(map.getKey(), jsonArray);
			// }
			// // System.out.println(jsonArray);
			// for (Entry<String, Object> map : ConntroJsonObject.entrySet()) {
			// if (!WhetherToEmpty.ObjectWhetherToEmpty(map.getValue())) {
			// if (IfName(map.getValue())) {
			// json.element(map.getKey(), JSONArray.fromObject(map.getValue()));
			// } else {
			// json.put(map.getKey(), map.getValue());
			// }
			// }
			// }

			for (Entry<String, List<?>> entry : ConntroJsonMap.entrySet()) {
				ConntroJsonObject.put(entry.getKey(), entry.getValue());
			}
			try {
				// string.append(JSON.toJSONString(ConntroJsonObject));
				string.append(JSON.toJSONString(ConntroJsonObject));
			} catch (Exception e) {
				Log4j.Log4JOutError(e.getMessage());
			}
			float in = string.toString().getBytes().length;
			Log4j.setLog4j(getClass());
			Log4j.Log4jOutInfo(in / 1024 + "K" + "  " + in / 1024 / 1024 + "M");
			out.print(string);
			// out.write(json.toString());
			/*
			 * char[] buf = json.toString().toCharArray(); out.write(buf);
			 */
			out.flush();
			out.close();
			ConntroJsonMap.clear();
			ConntroList.clear();
		} catch (IOException e) {
			Log4j.Log4JOutError(e.getMessage());
		}
	}

	public boolean IfName(Object object) {
		String nameString = object.getClass().getSuperclass().getName();
		String[] strings = new String[] { "java.util.AbstractMap", "java.util.AbstractList" };
		boolean bool = false;
		for (String string : strings) {
			if (string.equals(nameString)) {
				bool = true;
				break;
			}
		}
		return bool;
	}

	public Map<String, List<?>> getConntroJsonMap() {
		return ConntroJsonMap;
	}

	public Map<String, Object> getConntroJsonObject() {
		return ConntroJsonObject;
	}

	public void DMLDeleteOrUpdateOrAdd(boolean bool, DMLSuccessOrFailure SuccessInfo, DMLSuccessOrFailure Failure) {
		if (bool) {
			if (!WhetherToEmpty.ObjectWhetherToEmpty(SuccessInfo.getType())) {
				frameModel.SetSuccessInfo(SuccessInfo.getUrl(), SuccessInfo.getType());
			} else {
				frameModel.SetSuccessInfo(SuccessInfo.getUrl());
			}
		} else {
			if (!WhetherToEmpty.ObjectWhetherToEmpty(Failure.getType())) {
				frameModel.SetFailure(Failure.getUrl(), Failure.getType());
			} else {
				frameModel.SetFailure(Failure.getUrl());
			}
		}
	}
}
